/*******************************************************************************
 * 外语翻译平台(translate)
 * 
 * 成都一亿信息技术公司 版权所有
 * 
 * Copyright (C) 2010 Transer Information Technology Co., Ltd.
 * 
 * @Version : 1.0 2011-1-7 上午10:36:47
 * 
 * industry.js
 * 
 ******************************************************************************/

var pIndustry = [ {
	"id" : 40000,
	"name" : "科技类：",
	"parent" : 0
}, {
	"id" : 40001,
	"name" : "文学类：",
	"parent" : 0
}, {
	"id" : 40002,
	"name" : "法律类：",
	"parent" : 0
}, {
	"id" : 40003,
	"name" : "文教类：",
	"parent" : 0
}, {
	"id" : 40004,
	"name" : "经济类：",
	"parent" : 0
}, {
	"id" : 40005,
	"name" : "实用类：",
	"parent" : 0
} ];
var dIndustry = [ {
	"id" : 50000,
	"name" : "电子",
	"parent" : 40000
}, {
	"id" : 50001,
	"name" : "机械",
	"parent" : 40000
}, {
	"id" : 50002,
	"name" : "医药",
	"parent" : 40000
}, {
	"id" : 50003,
	"name" : "化学",
	"parent" : 40000
}, {
	"id" : 50004,
	"name" : "生物",
	"parent" : 40000
}, {
	"id" : 50005,
	"name" : "网络",
	"parent" : 40000
}, {
	"id" : 50006,
	"name" : "通讯",
	"parent" : 40000
}, {
	"id" : 50007,
	"name" : "小说",
	"parent" : 40001
}, {
	"id" : 50008,
	"name" : "电影",
	"parent" : 40001
}, {
	"id" : 50009,
	"name" : "诗歌",
	"parent" : 40001
}, {
	"id" : 50010,
	"name" : "散文",
	"parent" : 40001
}, {
	"id" : 50011,
	"name" : "学术论文/专著",
	"parent" : 40001
}, {
	"id" : 50012,
	"name" : "戏剧",
	"parent" : 40001
}, {
	"id" : 50013,
	"name" : "自传",
	"parent" : 40001
}, {
	"id" : 50014,
	"name" : "商业合同",
	"parent" : 40002
}, {
	"id" : 50015,
	"name" : "法律",
	"parent" : 40002
}, {
	"id" : 50016,
	"name" : "法规",
	"parent" : 40002
}, {
	"id" : 50017,
	"name" : "条例",
	"parent" : 40002
}, {
	"id" : 50018,
	"name" : "管理规定",
	"parent" : 40002
}, {
	"id" : 50019,
	"name" : "政府公文",
	"parent" : 40002
}, {
	"id" : 50020,
	"name" : "新闻",
	"parent" : 40003
}, {
	"id" : 50021,
	"name" : "报刊",
	"parent" : 40003
}, {
	"id" : 50022,
	"name" : "广告",
	"parent" : 40003
}, {
	"id" : 50023,
	"name" : "杂志",
	"parent" : 40003
}, {
	"id" : 50024,
	"name" : "教育",
	"parent" : 40003
}, {
	"id" : 50025,
	"name" : "广播",
	"parent" : 40003
}, {
	"id" : 50026,
	"name" : "金融",
	"parent" : 40004
}, {
	"id" : 50027,
	"name" : "保险",
	"parent" : 40004
}, {
	"id" : 50028,
	"name" : "人事",
	"parent" : 40004
}, {
	"id" : 50029,
	"name" : "财务",
	"parent" : 40004
}, {
	"id" : 50030,
	"name" : "市场",
	"parent" : 40004
}, {
	"id" : 50031,
	"name" : "销售手册",
	"parent" : 40004
}, {
	"id" : 50032,
	"name" : "备忘录",
	"parent" : 40004
}, {
	"id" : 50033,
	"name" : "海报",
	"parent" : 40004
}, {
	"id" : 50034,
	"name" : "宣传册",
	"parent" : 40004
}, {
	"id" : 50035,
	"name" : "公司简介",
	"parent" : 40004
}, {
	"id" : 50036,
	"name" : "产品介绍",
	"parent" : 40004
}, {
	"id" : 50037,
	"name" : "商业文书",
	"parent" : 40004
}, {
	"id" : 50038,
	"name" : "商品型录",
	"parent" : 40004
}, {
	"id" : 50039,
	"name" : "应用文",
	"parent" : 40005
}, {
	"id" : 50040,
	"name" : "个人简历",
	"parent" : 40005
}, {
	"id" : 50041,
	"name" : "成绩单",
	"parent" : 40005
}, {
	"id" : 50042,
	"name" : "证书",
	"parent" : 40005
}, {
	"id" : 50043,
	"name" : "来往信件",
	"parent" : 40005
}, {
	"id" : 50044,
	"name" : "邀请信",
	"parent" : 40005
}, {
	"id" : 50045,
	"name" : "推荐信",
	"parent" : 40005
}, {
	"id" : 50046,
	"name" : "出国申请",
	"parent" : 40005
} ];

function findIndustry(fid) {
	fid = parseInt(fid);
	
	if (isNaN(fid)) {
		return '不详';
	}

	var begin = 0;
	var end = dIndustry.length - 1;

	var mid = Math.floor((begin + end) / 2);
	var value = dIndustry[mid].id;

	var found = false;

	while (begin <= end) {
		if (value == fid) {
			found = true;
			break;
		} else if (value < fid) {
			begin = mid + 1;
		} else if (value > fid) {
			end = mid - 1;
		}

		mid = Math.floor((begin + end) / 2);
		value = dIndustry[mid].id;
	}

	return found ? dIndustry[mid].name : "不详";
}

function initIndustry(id, val) {
	var indus = $("#" + id);

	var part = new Array();

	part.push("<option value=''>请选择领域</option>");

	for ( var i = 0; i < dIndustry.length; i++) {
		if (val == '' + dIndustry[i].id) {
			part.push("<option value='" + dIndustry[i].id
					+ "' selected='selected'>" + dIndustry[i].name
					+ "</option>");
		} else {
			part.push("<option value='" + dIndustry[i].id + "'>"
					+ dIndustry[i].name + "</option>");
		}
	}

	indus.empty();
	indus.html(part.join(''));
}

function selectallindustry(obj) {
	try {
		diselectallindustry(obj);
		var i = 1;
		$(obj).parent().parent().find("input:checkbox").each(function (index, obj){
			if (i > 13) {
				return;
			}
			$(this).attr('checked', true);
			i++;
		});
	} catch (e) {
		alert(e);
	}
}

function diselectallindustry(obj) {
	$(obj).parent().parent().find("input:checked").each(function (index, obj){
		$(this).attr('checked', false);
	});
}

function checkAmount(obj) {
	if ($(obj).parent().parent().parent().find("input:checked").length > 13) {
		$(obj).attr('checked', false);
	}
}

function IndustryWindow(checkbox) {
	this.op = {
		resizable : false,
		modal : true,
		width : 'auto'
	};

	if (checkbox) {
		this.content = '<div id="lang" style="font-size:12px;" title="请选择你所擅长的领域(最多能选13个)">';
	} else {
		this.content = '<div id="lang" style="font-size:12px;" title="请选择你所擅长的领域">';
	}

	if (checkbox) {
		this.content += '<div style="width:100%;text-align:right;"><a style="cursor: pointer;" onclick="javascript:selectallindustry(this)">自动选择</a> <a style="cursor: pointer;" onclick="javascript:diselectallindustry(this)">取消选择</a></div>';

		for ( var index = 0; index < pIndustry.length; index++) {
			this.content += '<dl><dd><b>' + pIndustry[index].name + '</b>';
			for ( var childIndex = 0; childIndex < dIndustry.length; childIndex++) {
				if (dIndustry[childIndex].parent == pIndustry[index].id) {
					this.content += '<input type="checkbox" onclick="checkAmount(this)" value="'
							+ dIndustry[childIndex].id + '" /><span>'
							+ dIndustry[childIndex].name + '</span>';
				}
			}
			this.content += '</dd></dl>';
		}
	} else {
		for ( var index = 0; index < pIndustry.length; index++) {
			this.content += '<dl><dd><b>' + pIndustry[index].name + '</b>';
			for ( var childIndex = 0; childIndex < dIndustry.length; childIndex++) {
				if (dIndustry[childIndex].parent == pIndustry[index].id) {
					this.content += '<input type="radio" name="industry" value="'
							+ dIndustry[childIndex].id + '" /><span>'
							+ dIndustry[childIndex].name + '</span>';
				}
			}
			this.content += '</dd></dl>';
		}
	}

	this.content += '</div>';

	$(this.content).dialog({
		autoOpen : false,
		show : "blind",
		hide : "explode"
	});
}

IndustryWindow.prototype.show = function (callback, val) {
	if (typeof opt == 'function') {
		return;
	}
//	document.callback = callback;
	this.op.buttons = {
		'确定':function () {
//			document.callback(this, selectIndustry(this));
			callback(this, selectIndustry(this));
			$(this).dialog('close');
		},
		'取消':function () {
			$(this).dialog('close');
		}
	};
	
	preShow(this.content, val);
	
	$(this.content).dialog(this.op);
	
	$(this.content).dialog('open');
};

function preShow(newobj, val) {
	$(newobj).find(':checkbox').each(function (index, obj){
		if (val.indexOf($(obj).val()) > -1) {
			$(obj).attr('checked', true);
		}
	});
}

function selectIndustry(newobj) {
	var sels = [];
	$(newobj).find('input:checked').each(function (index, obj){
		var sel = {};
		sel.id = $(obj).val();
		sel.txt = $(obj).next('span').text();
		sels.push(sel);
	});
	return sels;
};
